package com.tony.blog.server.service.web;

import com.tony.blog.api.utils.Constant;
import com.tony.blog.model.entity.*;
import com.tony.blog.model.mapper.UserMsgMapper;
import com.tony.blog.model.request.CommentRequest;
import com.tony.blog.model.request.ConcernRequest;
import com.tony.blog.model.request.ReplyRequest;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: UserMsgService
 * @Auther: 皮蛋布丁
 * @Date: 2021/09/05/21:12
 * @Description: 用户消息服务
 */
@Service
public class UserMsgService {

    private static final Logger log = LoggerFactory.getLogger(UserMsgService.class);

    @Autowired
    private UserMsgMapper userMsgMapper;

    /**
    * @Description: sendMsg 通用消息记录
    * @Param: [userMsg]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/9/20 20:16
    */
    public void sendMsg(UserMsg userMsg) {
        try {
            //方式一：前端定时器轮询-pull模式
            userMsgMapper.insertSelective(userMsg);
        } catch (Exception e) {
            log.error("用户消息服务错误--发送");
        }
    }

    /**
    * @Description: forwardMsg 转发
    * @Param: [entity 用户, blogEntity 转发消息, blog 转发内容]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/9/20 20:03
    */
    @Async
    public void forwardMsg(SysUserEntity entity, BlogEntity blogEntity, BlogEntity blog) {
        try {
            String content = String.format(Constant.MsgModule.ForwardBlog.getContent(),entity.getName());
            UserMsg msg = new UserMsg(Constant.MsgModule.ForwardBlog.getCode(),blogEntity.getId(),content,0,
                    blog.getUserId().intValue(),new Date());
            this.sendMsg(msg);
        } catch (Exception e) {
            log.error("用户消息服务错误--转发消息");
        }
    }

    /**
    * @Description: commentMsg 评论
    * @Param: [request, id, user, srcBlog]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/9/21 21:24
    */
    @Async
    public void commentMsg(CommentRequest request, Integer id, SysUserEntity user, BlogEntity srcBlog) {
        try {
            String content = String.format(Constant.MsgModule.Comment.getContent(),user.getName(),request.getContent());
            UserMsg msg = new UserMsg(Constant.MsgModule.Comment.getCode(),id,content,0,
                    srcBlog.getUserId().intValue(),new Date());
            this.sendMsg(msg);
        } catch (Exception e) {
            log.error("用户消息服务错误--评论模块");
        }
    }

    /**
    * @Description: replyMsg 回复
    * @Param: [request, entity, id, userId]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/9/21 21:41
    */
    @Async
    public void replyMsg(ReplyRequest request, SysUserEntity entity, Integer id, Integer userId) {
        try {
            String content = String.format(Constant.MsgModule.Reply.getContent(),entity.getName(),request.getContent());
            UserMsg msg = new UserMsg(Constant.MsgModule.Reply.getCode(),id,content,0,userId,new Date());
            this.sendMsg(msg);
        } catch (Exception e) {
            log.error("用户消息服务错误--回复模块");
        }
    }

    /**
    * @Description: praiseMsg 点赞
    * @Param: [entity, srcBlog, praise]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/9/22 9:24
    */
    @Async
    public void praiseMsg(SysUserEntity entity, BlogEntity srcBlog, PraiseEntity praise) {
        try {
            String content = String.format(Constant.MsgModule.Praise.getContent(),entity.getName());
            UserMsg msg = new UserMsg(Constant.MsgModule.Praise.getCode(),praise.getId(),content,
                    0,srcBlog.getUserId().intValue(),new Date());
            this.sendMsg(msg);
        } catch (Exception e) {
            log.error("用户消息服务错误--点赞模块");
        }
    }

    /**
    * @Description: concernMsg 关注
    * @Param: [request, entity, concern]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/9/23 20:34
    */
    @Async
    public void concernMsg(ConcernRequest request, SysUserEntity entity, ConcernEntity concern) {
      try {
          String content = String.format(Constant.MsgModule.Concern.getContent(),entity.getName());

          UserMsg msg = new UserMsg(Constant.MsgModule.Concern.getCode(),concern.getId(),content,0,
                  request.getBeConcernUserId(),new Date());
          this.sendMsg(msg);
      } catch (Exception e) {
          log.error("用户消息服务错误--粉丝关注模块");
      }
    }
}
